package com.two.lbms.lyq.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.two.lbms.base.BaseService;
import com.two.lbms.dao.*;
import com.two.lbms.lyq.query.LoadDate;
import com.two.lbms.lyq.query.Queryreturn;
import com.two.lbms.lyq.query.Their;

import com.two.lbms.utils.AssertUtil;
import com.two.lbms.vo.BackList;
import com.two.lbms.vo.IllegalList;
import com.two.lbms.vo.Reader;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.security.cert.X509CRLEntry;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class RetrunBookService extends BaseService<Their,Integer> {

    @Resource
    private BackListMapper backListMapper;

    @Resource
    private ReaderMapper readerMapper;

    @Resource
    private  BookInfoMapper bookInfoMapper;
    @Resource
    private LendListMapper lendListMapper;  //查询是否逾期

    @Resource
    private AdminMapper adminMapper;
   /* @Resource
    private IllegalList illegalList; //如果逾期，需要添加违规记录*/


    public Map<String, Object> queryBackListByParams(Queryreturn queryreturn) {

        Map<String, Object> map = new HashMap<>();

        // 开启分页
        PageHelper.startPage(queryreturn.getPage(), queryreturn.getLimit());
        // 得到对应分页对象
        PageInfo<BackList> pageInfo = new PageInfo<>(backListMapper.selectByParams(queryreturn));

        // 设置map对象
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        // 设置分页好的列表
        map.put("data",pageInfo.getList());

        return map;
    }


    @Transactional(propagation = Propagation.REQUIRED)
    public void addbacklist(LoadDate loadDate) {
        AssertUtil.isTrue(null==loadDate.getBookName()||null==loadDate.getBookSituation()||null==loadDate.getReaderName()||null==loadDate.getAdminName(),"数据输入必填项有缺失");
        BackList backList = new BackList();
        //通过姓名获取id
        Integer readerId=readerMapper.selectByPrimaryName(loadDate.getReaderName()).getId();
        AssertUtil.isTrue(null==readerId,"当前读者不存在");
        //通过书籍名字获取id
        Integer bookId =bookInfoMapper.selectByPrimaryName(loadDate.getBookName()).getId();
        AssertUtil.isTrue(null==bookId,"当前图书不存在");
        //设置归还人
        backList.setReaderId(readerId);
        //设置归还书籍id
        backList.setBookId(bookId);
        //设置归还时间
        backList.setBackDate(LocalDateTime.now());
        //设置创建时间
        backList.setCreateDate(LocalDateTime.now());
        //设置最后更新时间
        backList.setUpdateDate(LocalDateTime.now());
        //设置管理员

        backList.setAdminName("张三");
        //是否到期

       /* lentQuery lentQuery=new lentQuery();
        lentQuery.setReaderId();
        lendListMapper.selectByParams(lentQuery);*/
       /* if(){
            backList.setIsDue("未逾期");
        }else {
            backList.setIsDue("已逾期");
        }*/

        //图书是否损坏
        backList.setBookSituation(loadDate.getBookSituation());
        if(loadDate.getBookSituation()=="已损坏"){
            //添加违规记录
            insertIllegalby();
        }

        //书架数量加一


        //设置借书时长
        backList.setLastTime(5);
        backList.setIsValid(1);
        //执行添加方法
        AssertUtil.isTrue(backListMapper.insertSelective(backList)!=1,"添加失败");

    }

    private void insertIllegalby() {

    }


    public Their selectByid(Integer BackListid) {
        Their their=backListMapper.selectByid(BackListid);
        return their;
    }


    @Transactional(propagation = Propagation.REQUIRED)
    public void updatebackList(LoadDate loadDate,Integer BackListId) {
        BackList backList=new BackList();
        backList.setId(BackListId);
        AssertUtil.isTrue(loadDate.getReaderName()==null&&loadDate.getReaderName()=="","读者姓名不能为空");
        AssertUtil.isTrue(loadDate.getBookName()==null&&loadDate.getBookName()=="","图书姓名不能为空");

        System.out.println("开始设置readerid");
        //判断当前读者是否存在,调用读者service，查看当前读者是否存在
        Reader reader = readerMapper.selectByPrimaryName(loadDate.getReaderName());
        //如果模糊查询用姓名查id,返回值不止一个，但是就不准确
        Integer readerId=reader.getId();

        System.out.println("开始设置bookid");
        Integer bookid=bookInfoMapper.selectByPrimaryName(loadDate.getBookName()).getId();

        //设置归还人
        backList.setReaderId(readerId);
        //设置归还书籍id
        backList.setBookId(bookid);

        //设置最后更新时间
        backList.setUpdateDate(LocalDateTime.now());
        AssertUtil.isTrue(adminMapper.selectByAdminName(loadDate.getAdminName())==null,"当前操作员不存在");
        //设置管理员
        backList.setAdminName("张三");//从cookie中获取
        //是否到期

       /* if(){
            backList.setIsDue("未逾期");
        }else {
            backList.setIsDue("已逾期");
        }*/

        //图书是否损坏
        backList.setBookSituation(loadDate.getBookSituation());
        if(loadDate.getBookSituation()=="已损坏"){
            //添加违规记录
            insertIllegalby();
        }

        //设置借书时长
        backList.setLastTime(5);

        AssertUtil.isTrue(backListMapper.updateByPrimary(backList)!=1,"修改失败");


    }




    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteByid(Integer[] BackListid) {
        // 判断ID是否为空
        AssertUtil.isTrue(null == BackListid || BackListid.length < 1, "待删除记录不存在！");
        // 执行删除（更新）操作，判断受影响的行数
        AssertUtil.isTrue(backListMapper.deleteBatch(BackListid) != BackListid.length, "图书借阅记录删除失败！");
    }
}
